# users/models.py

from datetime import datetime

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.safestring import mark_safe 

class Department(models.Model):
    #部门id/部门名称/是否现行/设置日期/备注
    department = models.CharField('部门名称',max_length=20,default='',blank=False)
    isactive = models.BooleanField('部门名称是否正在使用',default=False)
    addtime = models.DateTimeField('添加时间',default=datetime.now)
    mark = models.CharField('备注信息（500字）',max_length=500,default='',blank=True)

    class Meta:
        verbose_name = '部门信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.department
        
class UserProfile(AbstractUser):

    gender_choices = (
        ('male','男'),
        ('female','女')
    )

    nick_name = models.CharField('姓名',max_length=50,default='')
    birthday = models.DateField('生日',null=True,blank=True)
    gender = models.CharField('性别',max_length=10,choices=gender_choices,default='female')
    adress = models.CharField('地址',max_length=100,default='',blank=True)
    mobile = models.CharField('手机号',max_length=11,null=True,blank=True)
    image = models.ImageField(upload_to='image/%Y%m',default='image/default.png',max_length=100,blank=True)
    idcard = models.CharField('身份证号',max_length=24,null=True,blank=True)
    department = models.ForeignKey('users.Department',verbose_name='所属部门',null=True,on_delete=models.CASCADE)

    class Meta:
        verbose_name = '用户信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.nick_name

class EmailVerifyRecord(models.Model):
    send_choices = (
        ('register','注册'),
        ('forget','找回密码'),
        ('update_email','修改邮箱')
    )

    code = models.CharField('验证码',max_length=20)
    email = models.EmailField('邮箱',max_length=50)
    send_type = models.CharField(choices=send_choices,max_length=30)
    send_time = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name = '邮箱验证码'
        verbose_name_plural = verbose_name


class Banner(models.Model):
    title = models.CharField('标题',max_length=100)
    image = models.ImageField('轮播图',upload_to='banner/%Y%m',max_length=100)
    url = models.URLField('访问地址',max_length=200)
    index = models.IntegerField('顺序',default=100)
    add_time = models.DateTimeField('添加时间',default=datetime.now)

    class Meta:
        verbose_name = '轮播图'
        verbose_name_plural = verbose_name

class UserWork(models.Model):
    from project.models import ProjectList
    status_choices = (
        ('已完成','已完成'),
        ('任务取消','任务取消'),
        ('任务进行中','任务进行中'),
        ('任务暂停','任务暂停'),
        ('计划任务','计划任务')
    )
    add_time = models.DateTimeField('事件发生时间',null=True, blank=True,default=datetime.now)

    title = models.TextField('大事记内容',blank=False,default='发生时间：\n参加人员：\n地点：\n事件内容：\n结果：\n')
    status = models.CharField('完成情况',blank=False,choices=status_choices,max_length=50)
    project = models.ForeignKey(ProjectList, verbose_name='相关项目（可以为空）',related_name='project_to_log',null=True, blank=True,on_delete=models.DO_NOTHING)
    '''
    拟增加
    isopen:是否公开，一般项目记录要公开，个人事项隐藏。
    members:参加人
    '''
    isopen = models.BooleanField(verbose_name='是否公开(默认公开)',default=True)

    update_time = models.DateTimeField('最后一次更新时间',auto_now=True)
    user = models.ForeignKey('users.UserProfile',default='',related_name='user',verbose_name='添加人',blank=True,on_delete=models.DO_NOTHING)

    class Meta:
        verbose_name = '大事记'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return "%s的大事记"%(self.user.nick_name)

    def addproject_col(self):#增加立项列
        from project.models import AddProject,ProjectList
        '''
        显示所属大项目名称
        '''
        html = ''
        if self.project.pk:
            sub_col_name_list = AddProject.objects.filter(pk=self.project.addproject.pk)
            html = '<a href="/xadmin/users/userwork/?_p_project__addproject__tempname__contains=%s">%s</a>'%(sub_col_name_list[0],sub_col_name_list[0])
        else:
            html = '未选择项目'
        return mark_safe(html)
    addproject_col.short_description = '所属大项目'

    def serviceclass_col(self):#增加立项列
        from project.models import AddProject,ProjectList,ServiceClass
        '''
        显示所属专业
        '''
        html = '%s'%(self.project.serviceclass.name)
        return mark_safe(html)
    serviceclass_col.short_description = '填报专业'